﻿using System;

namespace LcfContols.DataBase
{
    /// <summary>
    /// 时间辅助类
    /// </summary>
    public class DateUtils
    {

        /// <summary>
        /// 返回时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static ulong GetTimeStamp(DateTime time)
        {
            return Convert.ToUInt64((time.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
        }


        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetTime(long timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = timeStamp * 10000000;
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// 几天之前或几天之后0点时间戳
        /// </summary>
        /// <returns></returns>
        public static ulong DayTimeStamp(int days = 0)
        {
            DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            return GetTimeStamp(today.AddDays(days));
        }

        /// <summary>
        /// 获取几个月前或几个月后1号0点时间戳
        /// </summary>
        /// <param name="months"></param>
        /// <returns></returns>
        public static ulong MonthTimeStamp(int months = 0)
        {
            DateTime m = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            return GetTimeStamp(m.AddMonths(months));
        }

    }
}
